package ru.mipt.sport.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 *
 * @author Danon
 */
public class StringUtils {
    public static final String DATE_FORMAT_STRING = "dd.MM.yyyy HH:mm";
    private static final DateFormat dateFormat = 
            new SimpleDateFormat(DATE_FORMAT_STRING, Locale.getDefault());
    
    public static Date toDate(String s) {
        if(s==null||s.isEmpty())
            return null;
        Date result = null;
        try {
            result = dateFormat.parse(s);
        } catch (Exception ex) {
            System.err.println("Failed to convert string "+s+" to date");
            System.err.println(ex.getLocalizedMessage());
        }
        return result; 
    }
    
    public static Date toDate(long v) {
        return new Date(v);
    }
    
    public static String toString(Date d) {
        if(d==null)
            return null;
        String result = null;
        try {
            result = dateFormat.format(d);
        } catch (Exception ex) {
            System.err.println("Failed to convert date "+d+" to string");
            System.err.println(ex.getLocalizedMessage());
        }
        return result; 
    }
}
